package com.tsmti.lab.controller.system;

import com.tsmti.core.common.controller.BaseCrudController;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.shiro.annotation.RequiresPathPermission;
import com.tsmti.system.entity.BillTemplate;
import com.tsmti.system.service.BillTemplateService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * @Package：com.tsmti.lab.controller.system
 * @Class：BillTemplateController
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/6/12 15:39
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
@RequestMapping("/system/billtemplate")
@RequiresPathPermission("sys:template")
public class BillTemplateController extends BaseCrudController<BillTemplate,Long> {
    @Autowired
    private BillTemplateService billTemplateService;

    @RequestMapping(method = RequestMethod.GET)
    @Override
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) {
        String firstDay = ConvertUtil.getFirstDayOfMonth();
        String lastDay = ConvertUtil.getLastDayOfMonth();
        BillTemplate billTemplate = new BillTemplate();
        List<BillTemplate> reportTemplateList = billTemplateService.list();
             model.addAttribute("list",reportTemplateList)
                .addAttribute("startTime", firstDay)
                .addAttribute("endTime", lastDay)
                .addAttribute("billTemplate",billTemplate);
        return display("template");
    }

    @RequestMapping(value="/save",method = RequestMethod.POST)
    @ResponseBody
    @Override
    public AjaxJson doSave(BillTemplate entity ,HttpServletRequest request, HttpServletResponse response,
                           BindingResult result) {
        AjaxJson ajaxJson = AjaxJson.success("保存成功");
        try{
            entity.setContent(HtmlUtils.htmlUnescape(entity.getContent()));
            billTemplateService.save(entity);
        }catch (Exception e){
            ajaxJson = AjaxJson.fail("保存失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }


    @RequestMapping(value = "/ajax/getReportTemplate",method = {RequestMethod.POST,RequestMethod.GET},produces = "text/html; charset=utf-8")
    @ResponseBody
    public String getReportTemplate(HttpServletRequest request, HttpServletResponse response) {
        Long templateId = ConvertUtil.getLongValue(request.getParameter("templateId"));
        try {
            BillTemplate billTemplate = billTemplateService.get(templateId);
            response.setContentType("text/html; charset=UTF-8");
            response.getWriter().write(ConvertUtil.null2String(billTemplate.getContent()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
